package gui;

import generators.MenuGenerator;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import parsers.MenuParser;

import types.MenuBar;
import writers.MenuesWriter;

import common.HelperMethods;
import common.Settings;

import java.awt.Rectangle;
import java.awt.Dimension;
import java.io.File;
import java.util.Vector;

public class FrmMenuesMain extends JDialog implements IObserver,ISubject
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton btnCreate = null;
	private JButton btnGenerate = null;
	private JButton btnLoad = null;
	
	
	private MenuBar bar = null; 
	private Vector<IObserver> observers = null;

	/**
	 * @param owner
	 */
	public FrmMenuesMain(/*Frame owner*/)
	{
		super(/*owner*/);
		observers = new Vector<IObserver>(1,1);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(300, 112);
		this.setTitle("Menues Generator");
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		setLocationRelativeTo(null);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBtnCreate(), null);
			jContentPane.add(getBtnGenerate(), null);
			jContentPane.add(getBtnLoad(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes btnCreate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCreate()
	{
		if (btnCreate == null)
		{
			btnCreate = new JButton();
			btnCreate.setBounds(new Rectangle(18, 26, 72, 26));
			btnCreate.setText("Create");
			btnCreate.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					openMenuesForm();
				}
			});
		}
		return btnCreate;
	}
	
	protected void openMenuesForm()
	{
		FrmMenues f = new FrmMenues();
		f.registerInterest(this);
		f.setVisible(true);
		
	}

	/**
	 * This method initializes btnGenerate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnGenerate()
	{
		if (btnGenerate == null)
		{
			btnGenerate = new JButton();
			btnGenerate.setBounds(new Rectangle(188, 26, 86, 26));
			btnGenerate.setText("Generate");
			btnGenerate.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					HelperMethods.deleteDir(new File(Settings.MenuesOutputDirectory));

					boolean success = (new File(Settings.MenuesOutputDirectory)).mkdirs();
					if (!success)
					{
						JOptionPane.showMessageDialog(null, "Error creating output directory.","Error",JOptionPane.ERROR_MESSAGE);
					}

					if (bar != null)
					{
						MenuGenerator g = new MenuGenerator();
						g.generate(bar);
						MenuesWriter w = new MenuesWriter();
						w.write(bar);
						
						JOptionPane.showMessageDialog(null, "Files generated successfuly.","Success",JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		}
		return btnGenerate;
	}

	/**
	 * This method initializes btnLoad	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnLoad()
	{
		if (btnLoad == null)
		{
			btnLoad = new JButton();
			btnLoad.setBounds(new Rectangle(108, 26, 62, 26));
			btnLoad.setText("Load");
			btnLoad.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(null);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
			            //File file = fc.getSelectedFile();
						String fileName = fc.getSelectedFile().getPath();
			            
			            MenuParser p = new MenuParser();
			    		MenuBar bar = p.parse(fileName);
			    		
			    		System.out.println(bar.getMenues().get(0).getLevel());
					}
				}
			});
		}
		return btnLoad;
	}

	@Override
	public void executeCommand(Object obj)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void takeResult(Object result, String type)
	{
		if("MenuBar".equals(type))
		{
			bar  = (MenuBar)result;
		}
	}

	@Override
	public void registerInterest(IObserver obs)
	{
		observers.addElement(obs);
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
